<template>
  <div id="mySpacePage">
    <p>正在跳转,请稍后...</p>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { listSpaceByPageUsingPost, listSpaceVoByPageUsingPost } from '@/api/spaceController.ts'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
import { SPACE_TYPE_ENUM } from '@/constants/space.ts'

const router = useRouter()
const loginUserStore = useLoginUserStore()

const checkUserSpace = async () => {
  //用户未登录，则直接跳转到登录页面
  const loginUser = loginUserStore.loginUser
  if (!loginUser?.id) {
    router.replace('/user/login')
    return
  }
  //如果用户已登录，会获取该用户以创建的空间
  const res = await listSpaceVoByPageUsingPost({
    userId: loginUser.id,
    current: 1,
    pageSize: 1,
    spaceType:SPACE_TYPE_ENUM.PRIVATE
  })
  if (res.data.code === 0) {
    //如果有进入第一个空间
    if (res.data.data?.records?.length > 0) {
      const space = res.data.data.records[0]
      router.replace(`/space/${space.id}`)
    } else {
      //如果没有跳转到创建页面空间
      router.replace('/add_space')
      message.warn('请先创建空间')
    }
  } else {
    message.error('加载空间失败' + res.data.message)
  }
}
//在加载是检查用户空间
onMounted(() => {
  checkUserSpace()
})
</script>
